﻿using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using rossisdead.Mvc.Detergent;
using Moq;

namespace MMSINC.CoreTest.ClassExtensions.MvcExtensions
{
    [TestClass]
    public class CascadingDropDownExtensionsTest
    {
        #region Fields

        private Mock<ICascadingDropDownExtensionsHelper> _helper;
        private SomeModel _model;

        #endregion

        #region Init/Cleanup

        [TestInitialize]
        public void InitializeTest()
        {
            _helper = new Mock<ICascadingDropDownExtensionsHelper>();
            CascadingDropDownExtensions.Helper = _helper.Object;
        }

        [TestCleanup]
        public void CleanupTest()
        {
            CascadingDropDownExtensions.Helper = null;
        }

        #endregion

        #region Tests

        #region Helper Property

        [TestMethod]
        public void TestHelperPropertyReturnsNewInstanceIfSetToNull()
        {
            var oldHelper = CascadingDropDownExtensions.Helper;
            CascadingDropDownExtensions.Helper = null;
            var helper = CascadingDropDownExtensions.Helper;
            Assert.AreNotSame(oldHelper, helper);
            Assert.IsInstanceOfType(helper, typeof (CascadingDropDownExtensionsHelper));
        }

        [TestMethod]
        public void TestHelperPropertyReturnsInstanceSetThroughSetter()
        {
            CascadingDropDownExtensions.Helper = _helper.Object;
            var helper = CascadingDropDownExtensions.Helper;
            Assert.AreSame(_helper.Object, helper);
        }
        
        #endregion

        [TestMethod]
        public void TestFinalCascadingDropDownMethodCallsExpectedMethodsOnHelper()
        {
            var htmlHelper = HtmlHelperExtensionsTest.GetHtmlHelper();
            var attrs = new RouteValueDictionary();
            var cascade = new CascadingAttribute();
            var list = new List<SelectListItem>();
            list.Add(new SelectListItem { Value= "Value", Text="Text" });

            _helper.Setup(x => x.GetCascadingAttribute(htmlHelper, "SomeProperty")).Returns(cascade).Verifiable();
            _helper.Setup(x => x.ApplyCascadingHtmlAttributes(cascade, attrs)).Verifiable();
            _helper.Setup(x => x.GetSelectListItems(htmlHelper, cascade)).Returns(list);
            htmlHelper.CascadingDropDown("SomeProperty", null, null, attrs);

            _helper.VerifyAll();
        }
        
        #endregion

        #region Helper classes
        
        private class SomeModel
        {
            public string SomeProperty { get; set; }
        }

        #endregion
    }
}
